using System;
using System.Globalization;

namespace ShopPowerTemplate.Common.Validation
{
    public class RequiredDateValidationControl : ShopPowerTemplate.Common.Validation.ValidationControl
    {
        public RequiredDateValidationControl()
            : base()
        {
            CultureInfo cultureInfo = CultureInfo.CurrentUICulture;
            ValidationMessage.Text = "*" + cultureInfo.DateTimeFormat.ShortDatePattern;
        }

        public override bool Validate()
        {
            bool result = true;
            if (PropertyToValidate != null)
            {
                if (PropertyToValidate is string)
                {
                    DateTime aux = DateTime.MinValue;
                    string dateString = ((string)PropertyToValidate);
                    if (!string.IsNullOrWhiteSpace(dateString))
                    {
                        CultureInfo cultureInfo = CultureInfo.CurrentUICulture;
                        result = DateTime.TryParse(dateString, cultureInfo.DateTimeFormat, DateTimeStyles.None, out aux);
                        if (aux != null && aux == DateTime.MinValue)
                            result = false;
                    }
                }
                else
                    result = PropertyToValidate is DateTime;
            }

            if (result)
            {
                ValidationMessage.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                return true;
            }
            else
            {
                ValidationMessage.Visibility = Windows.UI.Xaml.Visibility.Visible;
                return false;
            }
        }
    }
}
